local hx__xufa = fk.CreateSkill{
  name = "hx__xufa",
  tags = { Skill.Switch },
}


hx__xufa:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "^nullification",
  expand_pile = hx__xufa.name,
  handly_pile = true,
  prompt = function (self, player)
    if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
      return "#hx__xufa_put:::"..math.max((player:getHandcardNum() + 1) // 2, 1)
    else
      return "#hx__xufa_remove:::"..math.max((#player:getPile(hx__xufa.name) + 1) // 2, 1)
    end
  end,
  interaction = function(self, player)
    local all_choices = {}
    local choices = {}
    if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
      for _, id in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(id)
        if not table.contains(choices, card.name) and card.type ~= Card.TypeEquip and card.name ~= "nullification" then
          local tocard = Fk:cloneCard(card.name)
          table.insert(all_choices, tocard.name)
          table.insert(choices, tocard.name)
        end
      end
    else
      for _, id in ipairs(player:getPile(hx__xufa.name)) do
        local card = Fk:getCardById(id)
        if not table.contains(choices, card.name) and card.type ~= Card.TypeEquip and card.name ~= "nullification" then
          local tocard = Fk:cloneCard(card.name)
          table.insert(all_choices, tocard.name)
          table.insert(choices, tocard.name)
        end
      end
    end
    local names = player:getViewAsCardNames(hx__xufa.name, all_choices,nil,nil,{
      not_passive = true,
    })
    if #names == 0 then return end
    return UI.CardNameBox { choices = names, all_choices = all_choices }
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = hx__xufa.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    if player:getSwitchSkillState(self.name, true) == fk.SwitchYang then
      for _, id in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(id)
        if card.name == self.interaction.data then
          room:addCardMark(card, "hx__xufa-inhand", 1)
        end
      end
      local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "hx__xufa_active",
        prompt = "请选择至少"..math.max((player:getHandcardNum() + 1) // 2, 1).."张牌",
        cancelable = false,
      })
      if not (success and dat) then
        return hx__xufa.name
      end
      player:addToPile(hx__xufa.name, dat.cards, true, hx__xufa.name, player)
     -- dat.cards = player:getPile(hx__xufa.name)
      local id = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        skill_name = hx__xufa.name,
        cancelable = true,
        include_equip = true,
      })
      if #id > 0 then
        use.card = Fk:cloneCard(self.interaction.data)
        use.card:addSubcards(id)
        if room:askToChoice(player, {
          skill_name = hx__xufa.name,
          choices = {"yes","no"},
          cancelable = true,
          prompt = "是否将此【"..Fk:translate(self.interaction.data).."】的目标改为自己？",
        }) == "yes" then
          use.tos = {player}
        end
      else
        return ""
      end
    else
      for _, id in ipairs(player:getPile(hx__xufa.name)) do
        local card = Fk:getCardById(id)
        if card.name == self.interaction.data then
          room:addCardMark(card, "hx__xufa-inhand", 1)
        end
      end
      local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "hx__xufa_active",
        prompt = "请选择至少"..math.max((#player:getPile(hx__xufa.name) + 1) // 2, 1).."张牌",
        cancelable = false,
      })
      if not (success and dat) then
        return hx__xufa.name
      end
      room:moveCardTo(dat.cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, hx__xufa.name, hx__xufa.name, true, player)
      local id = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        skill_name = hx__xufa.name,
        cancelable = true,
        include_equip = true,
      })
      if #id > 0 then
        use.card = Fk:cloneCard(self.interaction.data)
        use.card:addSubcards(id)
      else
        return ""
      end
    end
  end,
  after_use = function(self, player, use)
    local room = player.room
    for _, id in ipairs(player:getCardIds("h")) do
      local card = Fk:getCardById(id)
      if card:getMark("hx__xufa-inhand") > 0 then
        room:setCardMark(card, "hx__xufa-inhand", 0)
      end
    end
    for _, id in ipairs(player:getPile(hx__xufa.name)) do
      local card = Fk:getCardById(id)
      if card:getMark("hx__xufa-inhand") > 0 then
        room:setCardMark(card, "hx__xufa-inhand", 0)
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(hx__xufa.name) == 0
  end,
})

hx__xufa:addEffect("targetmod", {
  bypass_distances = function(self, player, skill, card)
    return card and table.contains(card.skillNames, hx__xufa.name)
  end,
  bypass_times = function(self, player, skill, scope, card, to)
    return card and table.contains(card.skillNames, hx__xufa.name)
  end,
})

return hx__xufa
